package com.demo.core.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.core.dao.ConfigItemDao;
import com.demo.core.model.ConfigItem;
import com.demo.core.service.base.ConfigItemService;

/**
 * 站点配置子项服务实现类
 * 
 * @author ryuu.kk
 *
 */
public class ConfigItemServiceImpl implements ConfigItemService {

	/**
	 * 配置数据访问层Dao
	 */
	private ConfigItemDao configItemDao;
	
	@Override
	public Long save(ConfigItem item) {
		return configItemDao.insert(item);
	}
	
	@Override
	public boolean exists(Long id) {
		return configItemDao.exists(id);
	}
	
	@Override
	public void delete(Long id) {
		configItemDao.delete(id);
	}
	
	@Override
	public void deleteItems(Long configId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("configId", configId);
		configItemDao.delete(params);
	}
	
	@Override
	public void updateStatus(Long id, boolean enabled, String status) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		ConfigItem config = new ConfigItem();
		config.setEnabled(enabled);
		config.setStatus(status);
		configItemDao.updateStatus(config, params);
	}
	
	@Override
	public void update(Long id, String name, String value) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		ConfigItem configItem = new ConfigItem();
		configItem.setName(name);
		configItem.setValue(value);
		configItemDao.update(configItem, params);
	}
	
	@Override
	public void update(Long id, String name, String value, String text) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		ConfigItem configItem = new ConfigItem();
		configItem.setName(name);
		configItem.setValue(value);
		configItem.setText(text);
		configItemDao.update(configItem, params);
	}

	@Override
	public ConfigItem find(Long configId) {
		return configItemDao.select(configId);
	}

	@Override
	public List<ConfigItem> findAll(Long configId) {
		return findAll(configId, null);
	}

	@Override
	public List<ConfigItem> findAll(Long configId, String i18n) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("configId", configId);
		params.put("i18n", i18n);
		return configItemDao.select(params);
	}

	public void setConfigItemDao(ConfigItemDao configItemDao) {
		this.configItemDao = configItemDao;
	}

}